package org.example.demo;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ThreadLocalRandom;

public class Demo06 {
    /**
     * 函数化对象
     */
    interface Lambda {
        int calculate(int a, int b);
    }

    static class Server {
        public static void main(String[] args) throws IOException {
            // 创建服务端 端口号8080
            try (ServerSocket serverSocket = new ServerSocket(8080);) {
                System.out.println("server start ...");
                // 死循环接受消息
                while (true) {
                    Socket ss = serverSocket.accept();
                    // 异步接收消息
                    new Thread(
                            () -> {
                                try {
                                    // 获取输入流
                                    ObjectInputStream is = new ObjectInputStream(ss.getInputStream());
                                    // 解析出规则对象
                                    Lambda lambda = (Lambda) is.readObject();
                                    // 获取两个随机数
                                    int a = ThreadLocalRandom.current().nextInt(10);
                                    int b = ThreadLocalRandom.current().nextInt(10);
                                    // 打印不同规则的输出结果
                                    // %s 表示一个字符串string
                                    // %d 表示一个整数 byte short int long
                                    // %n 表示换行符
                                    // 注意这里使用的是 printf  不是println
                                    System.out.printf(
                                            "%s %d op %d = %d%n",
                                            ss.getRemoteSocketAddress().toString(),
                                            a,
                                            b,
                                            lambda.calculate(a, b));
                                } catch (IOException | ClassNotFoundException e) {
                                    throw new RuntimeException(e);
                                }
                            }
                    ).start();
                }

            }
        }
    }

    static class Client1 {
        public static void main(String[] args) {
            // 创建socket🔗
            try (Socket socket = new Socket("127.0.0.1", 8080);) {
                // 设置规则 并实现序列化
                Lambda lambda = (Lambda & Serializable) (a, b) -> a + b;
                // 创建输出流
                ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
                // 确定输出对象
                outputStream.writeObject(lambda);
                // 输出
                outputStream.flush();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }

    static class Client2 {
        public static void main(String[] args) {
            try (Socket socket = new Socket("127.0.0.1", 8080)) {
                Lambda lambda = (Lambda & Serializable) (a, b) -> a - b;
                ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
                outputStream.writeObject(lambda);
                outputStream.flush();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    static class Client3 {
        public static void main(String[] args) {
            try (Socket socket = new Socket("127.0.0.1", 8080)) {
                Lambda lambda = (Lambda & Serializable) (a, b) -> a * b;
                ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
                outputStream.writeObject(lambda);
                outputStream.flush();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
